package com.example.toolbar.fragment;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.ab.fragment.AbFragment;
import com.ab.http.AbHttpListener;
import com.ab.http.AbRequestParams;
import com.ab.util.AbToastUtil;
import com.ab.view.pullview.AbPullToRefreshView;
import com.ab.view.pullview.AbPullToRefreshView.OnFooterLoadListener;
import com.ab.view.pullview.AbPullToRefreshView.OnHeaderRefreshListener;
import com.ab.view.sliding.AbSlidingPlayView;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.toolbar.R;
import com.example.toolbar.adapter.ArticleListAdapter;
import com.example.toolbar.module.Article;
import com.example.toolbar.net.NetworkWeb;
import com.example.toolbar.utils.imgUtils;
import com.example.toolbar.widget.FloatingActionButton;

public class FragmentLoad extends AbFragment {

	private Activity mActivity = null;
	private List<Article> mList = null;
	private AbPullToRefreshView mAbPullToRefreshView = null;
	private ListView mListView = null;
	private int currentPage = 1;
	private ArticleListAdapter myListViewAdapter = null;
	private int total = 50;
	private int pageSize = 5;
	private String url = null;
	private String type = null;
	private AbSlidingPlayView mSlidingPlayView;
	LayoutInflater minflater;
	private FloatingActionButton floatingActionButton;
	private String status = null;
	private boolean is_lunbo;
	private boolean is_floatbtn;
	private AbRequestParams params;
	private boolean is_fact;
	public FragmentLoad() {
		super();
	}

	public FragmentLoad(AbRequestParams params, String url, boolean is_lunbo,
			boolean is_floatbtn,boolean is_fact) {
		this.params = params;
		this.url = url;
		this.is_lunbo = is_lunbo;
		this.is_fact=is_fact;
	}

	@Override
	public View onCreateContentView(LayoutInflater inflater,
			ViewGroup container, Bundle savedInstanceState) {

		mActivity = this.getActivity();
		minflater = mActivity.getLayoutInflater();
		View view = inflater.inflate(R.layout.pull_to_refresh_list, null);
		floatingActionButton = (FloatingActionButton) view
				.findViewById(R.id.button_floating_action);
		// 获取ListView对象
		mAbPullToRefreshView = (AbPullToRefreshView) view
				.findViewById(R.id.mPullRefreshView);
		mListView = (ListView) view.findViewById(R.id.mListView);
			floatingActionButton.attachToListView(mListView);
		OnClickListener listener = new FloatLisener();
		floatingActionButton.setOnClickListener(listener);
		// 设置监听器
		mAbPullToRefreshView
				.setOnHeaderRefreshListener(new OnHeaderRefreshListener() {

					@Override
					public void onHeaderRefresh(AbPullToRefreshView view) {
						refreshTask(params);
					}
				});
		mAbPullToRefreshView
				.setOnFooterLoadListener(new OnFooterLoadListener() {

					@Override
					public void onFooterLoad(AbPullToRefreshView view) {
						loadMoreTask(params);

					}
				});

		// 设置进度条的样式
		mAbPullToRefreshView.getHeaderView().setHeaderProgressBarDrawable(
				this.getResources().getDrawable(R.drawable.progress_loading2));
		mAbPullToRefreshView.getFooterView().setFooterProgressBarDrawable(
				this.getResources().getDrawable(R.drawable.progress_loading2));

		// ListView数据
		mList = new ArrayList<Article>();

		// 使用自定义的Adapter
		myListViewAdapter = new ArticleListAdapter(mActivity, mList);
		mSlidingPlayView = new AbSlidingPlayView(mActivity);

		final View mPlayView = minflater.inflate(R.layout.play_view_item, null);
		ImageView mPlayImage = (ImageView) mPlayView
				.findViewById(R.id.mPlayImage);
		TextView mPlayText = (TextView) mPlayView.findViewById(R.id.mPlayText);
		mPlayText.setText("公安部门打击足球腐败决心大");
		mPlayImage.setBackgroundResource(R.drawable.xinwen);

		final View mPlayView1 = minflater
				.inflate(R.layout.play_view_item, null);
		ImageView mPlayImage1 = (ImageView) mPlayView1
				.findViewById(R.id.mPlayImage);
		TextView mPlayText1 = (TextView) mPlayView1
				.findViewById(R.id.mPlayText);
		mPlayText1.setText("中国人民解放军演习");
		mPlayImage1.setBackgroundResource(R.drawable.xinwen2);

		final View mPlayView2 = minflater
				.inflate(R.layout.play_view_item, null);
		ImageView mPlayImage2 = (ImageView) mPlayView2
				.findViewById(R.id.mPlayImage);
		TextView mPlayText2 = (TextView) mPlayView2
				.findViewById(R.id.mPlayText);
		mPlayText2.setText("法国黑手党大佬去世，葬礼堪比电影");
		mPlayImage2.setBackgroundResource(R.drawable.xinwen3);

		mSlidingPlayView.setNavHorizontalGravity(Gravity.RIGHT);
		mSlidingPlayView.addView(mPlayView);
		mSlidingPlayView.addView(mPlayView1);
		mSlidingPlayView.addView(mPlayView2);
		mSlidingPlayView.startPlay();
		// 设置高度
		mSlidingPlayView.setLayoutParams(new AbsListView.LayoutParams(
				LayoutParams.FILL_PARENT, 300));
		if (is_lunbo) {
			mListView.addHeaderView(mSlidingPlayView);
		}
		// 解决冲突问题
		mSlidingPlayView.setParentListView(mListView);
		mListView.setAdapter(myListViewAdapter);
		// item被点击事件
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
			}
		});

		// 加载数据必须
		this.setAbFragmentOnLoadListener(new AbFragmentOnLoadListener() {

			@Override
			public void onLoad() {
				// 第一次下载数据
				refreshTask(params);
			}

		});

		return view;
	}

	@Override
	public void setResource() {
		// 设置加载的资源
		this.setLoadDrawable(R.drawable.ic_load);
		this.setLoadMessage("正在查询,请稍候");

		this.setRefreshDrawable(R.drawable.ic_load);
		this.setRefreshMessage("请求出错，请重试");
	}

	/**
	 * 下载数据
	 */
	public void refreshTask(AbRequestParams params) {
		currentPage = 1;
		// 绑定参数

		params.put("page", String.valueOf(currentPage));
		params.put("rows", String.valueOf(pageSize));

		// 下载网络数据
		NetworkWeb web = NetworkWeb.newInstance(mActivity);
		web.findLogList(params, new AbHttpListener() {

			@Override
			public void onSuccess(String content) {
				ArrayList<Article> newList = parseData(content);
				mList.clear();
				if (newList != null && newList.size() > 0) {
					mList.addAll((List<Article>) newList);
					myListViewAdapter.notifyDataSetChanged();
					newList.clear();
				}
				mAbPullToRefreshView.onHeaderRefreshFinish();
				showContentView();
				// 模拟用，真是开发中需要直接调用run内的内容
				/*
				 * new Handler().postDelayed(new Runnable() {
				 * 
				 * @Override public void run() { // 显示内容 showContentView(); }
				 * 
				 * }, 3000);
				 */

			}

			@Override
			public void onFailure(String content) {
				AbToastUtil.showToast(mActivity, content);
				// 显示重试的框
				showRefreshView();
			}

		}, url);
	}

	public void loadMoreTask(AbRequestParams params) {
		currentPage++;
		// 绑定参数

		params.put("page", String.valueOf(currentPage));
		params.put("rows", String.valueOf(pageSize));

		// 下载网络数据
		NetworkWeb web = NetworkWeb.newInstance(this.getActivity());
		web.findLogList(params, new AbHttpListener() {

			@Override
			public void onSuccess(String content) {
				ArrayList<Article> newList = parseData(content);
				if (newList != null && newList.size() > 0) {
					mList.addAll((List<Article>) newList);
					myListViewAdapter.notifyDataSetChanged();
					newList.clear();
				}
				mAbPullToRefreshView.onFooterLoadFinish();
			}

			@Override
			public void onFailure(String content) {

			}

		}, url);
	}

	private ArrayList<Article> parseData(String content) {
		JSONObject object = JSON.parseObject(content);
		JSONArray data = object.getJSONArray("data");
		ArrayList<Article> articles = new ArrayList<Article>();
		for (int i = 0; i < data.size(); i++) {
			JSONObject curResult = data.getJSONObject(i);
			String tilte = curResult.getString("title");
			String time = curResult.getString("dateline");
			String ims = curResult.getString("images");
			String[] urls = new String[] {};
			String source = "";
			if (ims != null) {
				urls = imgUtils.getImageUrl(ims);
				source = curResult.getString("source");
			} else {
				source = "";
			}
			String commentNum = curResult.getString("commentNum");
			String id = curResult.getString("id");
			String facts = curResult.getString("fact");
			String views = curResult.getString("views");
			String good = curResult.getString("good");
			String bad = curResult.getString("bad");
			articles.add(new Article(id, tilte, urls, time, commentNum, facts,
					source, views, good, bad));
		}
		return articles;
	}

	class FloatLisener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (v.getId() == R.id.button_floating_action) {
				Log.d("---------", "啊啊啊啊啊");
			}
		}

	}
}
